/**
 * 
 */
package t4j.generics;

import java.util.Collection;

import t4j.net.mindview.util.Generator;

//接口
interface Addable<T> {
	void add(T t);
}


//类生成器
public class Fill2 {

	//利用发射来生成
	public static <T> void fill(Addable<T> addable,Class<? extends T> classToken,int size){
		for(int i = 0; i < size ; i++){
			try {
				addable.add(classToken.newInstance());
			} catch (Exception e) {
			
				throw new RuntimeException(e);
			} 
		}
	}
	
	public static <T> void fill(Addable<T> addable,Generator<T> generator,int size){
		for(int i = 0;i < size;i++)
			addable.add(generator.next());
	}
}
//适配器1
class AddableCollectionAdapter<T> implements Addable<T>{

	private Collection<T> c;
	
	public AddableCollectionAdapter(Collection<T> c){
		this.c = c;
	}
	
	@Override
	public void add(T t) {
		c.add(t);
	}
}

//适配器2，使用泛型方法，这是产生更优雅的代码的一种惯用技巧
class Adapter{
	public static <T> Addable<T> collectionAdapter(Collection<T> c){
		return new AddableCollectionAdapter<T>(c);
	}
}

//使用继承来创建这个适配器3
class AddableSimpleQueue<T> extends SimpleQueue<T> implements Addable<T>{
	public void add(T item){
		super.add(item);
	}
}


